% *****************************************************************************
%%                                                                            *
%%                                                                            *
% AUTHOR: Vigneshwar Ramakrishnan											  *	
% Date created: 29 August 2022												  *	
% CC-BY-SA-NC                                                                 *
%Description: this file is required for translational_entropy.m               *
%******************************************************************************

function [final] = newton(x0,d)
% Newton method to solve non-linear algebraic equation

iter = 1;
x = x0;
del =  d; 
 
f = 2*(del^(-9/2))*(x^(15/2)) - 6*(del^(-3))*(x^5) - (del^(-3/2))*(x^(7/2)) + 6*(del^(-3/2))*(x^(5/2)) + 2*x - 2;

[xvals]=[x];

while abs(f)>1e-8

    df = 2*(del^(-9/2))*(15/2)*(x^6.5) - 6*(del^(-3))*5*(x^4) - (del^(-3/2))*(7/2)*(x^2.5) + 6*(del^(-3/2))*(5/2)*(x^1.5) + 2;
    
    x_new = x-(f/df);
    
    f = 2*(del^(-9/2))*(x_new^(15/2)) - 6*(del^(-3))*(x_new^5) - (del^(-3/2))*(x_new^(7/2)) + 6*(del^(-3/2))*(x_new^(5/2)) + 2*x_new - 2;
    iter = iter+1;
    xvals = [xvals x_new];
    final = x_new;
    x = x_new;
    
end

% if isreal(xvals)==1
%     plot(1:iter,xvals)
%     xlabel('Iteration #')
%     ylabel ('Solution Trajectory')
% end
% if isreal(xvals)==0
%     
%     plot(xvals,'r')
%     xlabel('Real Part')
%     ylabel('Imaginary Part')
%     hold on
%     plot(xvals(1),'r*')
%     plot(xvals(end),'b*')
%     hold off
% end

return

